-- FOREIGN DATABASE

-- Table: public.foreign_table
CREATE TABLE public.foreign_table
(
  fkey_uuid uuid NOT NULL,
  primary_uuid uuid NOT NULL,
  CONSTRAINT pkey PRIMARY KEY (primary_uuid)
);

CREATE INDEX fkey_uuid_idx
  ON public.foreign_table
  USING btree
  (fkey_uuid);

-- Table: public.foreign_filter_table

CREATE TABLE public.foreign_filter_table
(
  primary_uuid uuid NOT NULL,
  filter_uuid uuid,
  CONSTRAINT filter_table_pk PRIMARY KEY (primary_uuid)
)

CREATE INDEX filter_uuid_idx
  ON public.foreign_filter_table
  USING btree
  (filter_uuid);


-- LOCAL DATABASE
-- Table: public.local_table

CREATE TABLE public.local_table
(
  fkey_uuid uuid NOT NULL,
  primary_uuid uuid NOT NULL,
  CONSTRAINT pkey PRIMARY KEY (primary_uuid)
)

CREATE INDEX fkey_uuid_idx
  ON public.local_table
  USING btree
  (fkey_uuid);


-- DATA Generation
create extension "uuid-ossp";

insert into foreign_filter_table
  select uuid_generate_v4(), uuid_generate_v4()
    FROM generate_series(1,100000)

insert into local_table (primary_uuid, fkey_uuid)
  select uuid_generate_v4(), primary_uuid from
    (
    select primary_uuid
    from foreign_server.foreign_filter_table
	where (('x'||substr(md5(primary_uuid::text),1,8))::bit(32)::int)%20 = 0
    )a,
    (
	select 0
	from generate_series(0, 20)
    )b


insert into foreign_server.foreign_table (primary_uuid, fkey_uuid)
  select uuid_generate_v4(), primary_uuid from
    (
    select primary_uuid
    from foreign_server.foreign_filter_table
	where (('x'||substr(md5(primary_uuid::text),1,8))::bit(32)::int)%20 = 0
    )a,
    (
	select 0
	from generate_series(0, 20)
    )b
